#pragma once

#include "type.h"
#include "Chunk.h"
#include "defaultworkqueue.h"
#include "ChunkLoader.h"

#include "utility.h"

//class hkpWorld;
//class hkMemoryRouter;

namespace ChunkSystem
{
	class ChunkFileSerializer;
	class ChunkSpace;
	class ChunkCamera;
	class ChunkCameraFactory;
	class ChunkSceneManager;

	class IRenderer;

	class ChunkManager
		:public Singleton< ChunkManager >
	{
	public:
		typedef std::vector<Chunk*>	LoadingQueue;

		void ChangeChunkSpace( const String& strChunkSpace );
		//if the factory is none, return current camera.
		ChunkCamera* GetChunkCamera( ChunkCameraFactory* pCamFactory );

		ChunkSpace* GetCurChunkSpace( void ) const;

		void Tick(void);
		void Draw(void);

		void AbortAllChunkRequest( void );

		static ChunkManager& GetInstance( void );

		ChunkFileSerializer* GetChunkSerialzer( void );

		void SetResPath( const String& strResPath );
		String GetResPath( void ) const;

		void SetSpacePath( const String& strSpacePath );
		String GetSpacePath( void ) const;

		//hkpWorld* GetCurPhysicsWorld( void ) const;

		//for chunk system usage.
		void LoadChunk( Chunk* pChunk );

		ChunkSceneManager* GetChunkSceneManager( void ) const;

	private:

		ChunkManager(void);
		~ChunkManager(void);

		void	_UpdateLoadingQueue( void );
		void	_SolveBlind( const Common::Vector3& rPos , ChunkSpace* pCameraChunkSpace );
		void	_Scan( void );

	private:

		ChunkSpace*			m_pWorkingChunkSpace;
		ChunkCamera*		m_pCurCamera;

		String				m_strResPath;
		String				m_strSpacePath;

		LoadingQueue		m_loadingQueue;
		
		//used for recording the result of funtion _SolveBlind seeking
		ChunkSeeker			m_ChunkSeeker;
		ChunkLoader			m_ChunkLoader;

		Common::Vector3		m_vLastScanPos;
		bool				m_bNoneLoadedAtLastScan;

		Common::DefaultWorkQueue		m_workqueue;

		ChunkSceneManager*	m_pChunkSceneManager;

		//physics, will be removed to another project
		//hkpWorld*			m_pWorld;
		//hkMemoryRouter*		m_pMemoryRouter;
	};

}